<?php

declare(strict_types=1);
/**
 * Author: Zane
 * Email: 873934580@qq.com
 * Date: 2023/7/13.
 */

namespace App\Service;

use App\Enum\StatusEnum;
use App\Models\Category;
use App\Service\Contract\CategoryInterface;
use Zane\View\Service\Transfer;

class CategoryService implements CategoryInterface
{
    public function getAll(): array
    {
        $dao = Category::query()
            ->with('model:id,title')
            ->orderBy('weight', 'desc')
            ->orderBy('id', 'desc')->get();

        return $dao->toArray();
    }

    public function read(Transfer $transfer): array
    {
        $model = Category::query();
        if ($pid = $transfer->get('pid')) {
            $model->where('parent_id', (int) $pid);
        }
        $model->where('status', StatusEnum::ENABLE)
            ->orderBy('weight', 'desc')->orderBy('id', 'asc');

        return $model->get()->toArray();
    }
}
